angular.module('start.user.ctrl', [])

  .controller('UserCtrl', function($scope, $rootScope, $ionicModal, $$AuthService, $$MessageService, $location, $$UserDataService, $timeout, $$LocalStorage, $ionicLoading, $ionicPopup) {
    console.info("UserCtrl");

    $scope.$on("$ionicView.beforeEnter", function() { //每次进入页面前判断是否登录
      console.log("$ionicView.beforeEnter------LoginCtrl");
      //判断登录代码
      // $ionicHistory.clearCache() ;
      if ($rootScope.config.isLogined) {
        // 查看用户信息
        $$MessageService.getusernotify()
          .then(function(res) {
            if (res.data == null || res.data == undefined) {
              res.data = [];
            }
            var i = 0;
            for (i = 0; i < res.data.length; i++) {
              // 存在未读信息
              if (res.data[i].state == 0) {
                $(".u_messagetip").show();
                return;
              }
            }
            if (i == res.data.length) {
              $(".u_messagetip").hide();
              return;
            }
          });
        // 获取用户个人信息
        $$AuthService.getuserInformation()
          .then(function(res) {
            if (res.errCode == 203) {
              $rootScope.userData.attentions = res.data.attentions;
              $rootScope.userData.fans = res.data.fans;
              $rootScope.userData.headPic = res.data.headPic;
              $rootScope.userData.id = res.data.id;
              $rootScope.userData.isRealNameAuthentication = res.data.isRealNameAuthentication;
              $rootScope.userData.signature = res.data.signature;
              $rootScope.userData.telephone = res.data.telephone;
              $rootScope.userData.type = res.data.type;
              $rootScope.userData.userName = res.data.userName;

              $$UserDataService.updateUserData($rootScope.userData);
            }

          });

      } else {
        $(".u_messagetip").hide();
      }
    });

    // console.info("UserCtrl" + angular.element("#abc").html());
    $scope.$on("$ionicView.loaded", function() {
      console.info("UserCtrl--$ionicView.loaded ");
    });

    // 账号信息
    $scope.gouseredit = function() {
      if ($rootScope.needLogin() == false) {
        return;
      }
      $location.path("useredit");
    };

    // 消息通知
    $scope.gomessagelist = function() {
      if ($rootScope.needLogin("messagelist") == false) {
        return;
      }
      $location.path("messagelist");
    };

    // 我的订单
    $scope.goorderway = function() {
      if ($rootScope.needLogin("orderway") == false) {
        return;
      }
      $location.path("orderway");
    };

    // 我的钱包
    $scope.gomoney = function() {
      if ($rootScope.needLogin("money") == false) {
        return;
      }
      $location.path("money");
    };

    // 我的认证
    $scope.goauthselect = function() {
      if ($rootScope.needLogin("authselect") == false) {
        return;
      }
      $location.path("authselect");
    };
    // 平台声明
    // 我要分享
    // 系统设置

    $scope.shareAPP = function() {
      $scope.usersharemodal.show();
    };

    // 去登录
    $scope.gologin = function() {
      $location.path("login");
    };

    // 进入关注列表
    $scope.goattentionlist = function() {
      if ($rootScope.needLogin("attentionlist") == false) {
        return;
      }
      $location.path("attentionlist");
    };

    // 进入粉丝列表
    $scope.gofanslist = function() {
      if ($rootScope.needLogin("fanslist") == false) {
        return;
      }
      $location.path("fanslist");
    };

    var scope = "snsapi_userinfo",
      state = "_" + (+new Date());


    // 退出登录
    $scope.loginOut = function() {
      $ionicPopup.confirm({
        title: '退出登录',
        template: '确认退出登录？',
        cancelText: '取消',
        okText: "确定"
      }).then(function(res) {
        if (res) {
          $$UserDataService.clearUserData();
          // $$LocalStorage.remove("userData");
          // $rootScope.userData = $$UserDataService.getCacheUserData();
          // $rootScope.isLogined();
          // console.log($rootScope.userData);
          $(".u_messagetip").hide();
          $ionicLoading.show({
            template: "已退出登录",
            noBackdrop: true,
            duration: 1000
          });
          // // 如果是app端
          // if ($rootScope.config.isWebView) {
          //     // 设计极光推送别名为nologin
          //     $$JPushService.setAlias("nologin");
          // }
          $location.path("tab/user");
        } else {
          console.log('取消退出登录');
        }
      });
    };

    $scope.usersharemodal = {};
    $ionicModal.fromTemplateUrl('userShare.html', {
      scope: $scope,
      animation: 'slide-in-top'
    }).then(function(modal) {
      $scope.usersharemodal = modal;
      // $scope.usersharemodal.show();
    });


    //监听离开页面时关闭model
    $scope.$on("$ionicParentView.beforeLeave", function() {
      console.info("TrafficSignCtrl--$ionicView.beforeLeave ");
      $scope.usersharemodal.hide();
    });

    $scope.usersharemodalshow = function(wayid) {
      $scope.usersharemodal.show();

    };

    $scope.closeuserShareModel = function() {
      $scope.usersharemodal.hide();
    };


    $scope.share_wechat = function() {
      $ionicLoading.show({
        noBackdrop: true,
        duration: 10000
      });
      document.addEventListener("deviceready", function() {
        Wechat.share({
          message: {
            title: "伴你同游",
            description: "我们在寻找志同﻿道合，爱好旅游的你，旅游挣钱交友三不误",
            thumb: "https://www.bannitongyou.com/uploads/logo-100-blank.jpg",
            mediaTagName: "",
            messageExt: "",
            messageAction: "<action>dotalist</action>",
            media: {
              type: Wechat.Type.WEBPAGE,
              webpageUrl: "https://www.bannitongyou.com"
            }
          },
          scene: Wechat.Scene.SESSION // share to SESSION
        }, function() {
          $ionicLoading.hide();
        }, function(reason) {
          $ionicLoading.hide();
        });
        $ionicLoading.show({
          noBackdrop: true,
        });
      });
    };


    $scope.share_wechat_life = function() {
      $ionicLoading.show({
        noBackdrop: true,
        duration: 10000
      });
      document.addEventListener("deviceready", function() {
        Wechat.share({
          message: {
            title: "伴你同游",
            description: "我们在寻找志同﻿道合，爱好旅游的你，旅游挣钱交友三不误",
            thumb: "https://www.bannitongyou.com/uploads/logo-100-blank.jpg",
            mediaTagName: "",
            messageExt: "",
            messageAction: "<action>dotalist</action>",
            media: {
              type: Wechat.Type.WEBPAGE,
              webpageUrl: "https://www.bannitongyou.com"
            }
          },
          scene: Wechat.Scene.TIMELINE // share to Timeline
        }, function() {
          $ionicLoading.hide();
        }, function(reason) {
          // alert("Failed: " + reason);
          $ionicLoading.hide();
        });
        $ionicLoading.show({
          noBackdrop: true,
        });
      });
    };


    $scope.share_qq = function() {
      $ionicLoading.show({
        noBackdrop: true,
        duration: 10000
      });
      document.addEventListener("deviceready", function() {
        var args = {};
        args.client = QQSDK.ClientType.QQ; //QQSDK.ClientType.QQ,QQSDK.ClientType.TIM;
        args.scene = QQSDK.Scene.QQ; //QQSDK.Scene.QQZone,QQSDK.Scene.Favorite
        args.url = "https://www.bannitongyou.com";
        args.title = '伴你同游';
        args.description = '我们在寻找志同﻿道合，爱好旅游的你，旅游挣钱交友三不误';
        args.image = 'https://www.bannitongyou.com/uploads/logo-100-blank.jpg';
        QQSDK.shareNews(function() {
          $ionicLoading.hide();
        }, function(failReason) {
          // alert(failReason);
          $ionicLoading.hide();
        }, args);
        $ionicLoading.show({
          noBackdrop: true,
        });
      });
    };


    $scope.share_weibo = function() {
      $ionicLoading.show({
        noBackdrop: true,
        duration: 10000
      });
      document.addEventListener("deviceready", function() {
        var args = {};
        args.url = "https://www.bannitongyou.com";
        args.title = '伴你同游';
        args.description = '我们在寻找志同﻿道合，爱好旅游的你，旅游挣钱交友三不误';
        args.image = 'https://www.bannitongyou.com/uploads/logo-100-blank.jpg';
        WeiboSDK.shareToWeibo(function() {
          $ionicLoading.hide();
        }, function(failReason) {
          // alert(failReason);
          $ionicLoading.hide();
        }, args);
        $ionicLoading.show({
          noBackdrop: true,
        });
      });
    };


  });
